﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.Business.WebsiteBusinessCategory;
using BCData.Common.Business.WebsiteBusinessIntroduction;
using BCData.Common.Business.WebsiteBusinessTagcode;
using BCDto.Common.Business.WebsiteBusinessCategory;
using BCDto.Sim.Admin.Admin;
using BCEntity.Common.Business.WebsiteBusinessCategory;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.Common.Business.WebsiteBusinessCategory
{
    public class WebsiteBusinessCategoryService : IWebsiteBusinessCategoryService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IWebsiteBusinessTagcodeData websiteBusinessTagcodeData; 
        private readonly IWebsiteBusinessCategoryData websiteBusinessCategoryData;
        private readonly IWebsiteBusinessIntroductionData websiteBusinessIntroductionData;
        private readonly string key = string.Format("{0}", EntityNameConst.WebsiteBusinessCategoryEntity);

        public WebsiteBusinessCategoryService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IWebsiteBusinessTagcodeData websiteBusinessTagcodeData,
            IWebsiteBusinessCategoryData websiteBusinessCategoryData,
            IWebsiteBusinessIntroductionData websiteBusinessIntroductionData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.websiteBusinessTagcodeData = websiteBusinessTagcodeData;
            this.websiteBusinessCategoryData = websiteBusinessCategoryData;
            this.websiteBusinessIntroductionData = websiteBusinessIntroductionData;
        }

        #region 检查官网业务分类是否存在
        public bool Exists(int websiteBusinessCategoryId)
        {
            return this.websiteBusinessCategoryData.Exists(websiteBusinessCategoryId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加官网业务分类
        public WebsiteBusinessCategoryDto Add(WebsiteBusinessCategoryRequestDto requestDto,AdminDto adminDto)
        {
            if (!this.websiteBusinessTagcodeData.Exists(requestDto.WebsiteBusinessTagcode).Result)
            {
                throw new ArgumentException("此官网业务标记码不存在!");
            }
            var entity = requestDto.As<WebsiteBusinessCategoryEntity>();
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;

            return this.websiteBusinessCategoryData.Add(entity).GetAwaiter().GetResult().As<WebsiteBusinessCategoryDto>();
        }
        #endregion

        #region 修改官网业务分类
        public WebsiteBusinessCategoryDto Update(WebsiteBusinessCategoryPutDto putDto, AdminDto adminDto)
        {
            if (!this.Exists(putDto.WebsiteBusinessCategoryId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            if (!this.websiteBusinessTagcodeData.Exists(putDto.WebsiteBusinessTagcode).Result)
            {
                throw new ArgumentException("此官网业务标记码不存在!");
            }
            var entity = putDto.As<WebsiteBusinessCategoryEntity>();
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            var result = this.websiteBusinessCategoryData.Update(entity).GetAwaiter().GetResult().As<WebsiteBusinessCategoryDto>();
            string cacheKey = this.redisService.GetKey(key, entity.WebsiteBusinessCategoryId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 删除官网业务分类
        public bool Delete(int websiteBusinessCategoryId)
        {
            if (!this.Exists(websiteBusinessCategoryId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            if (this.websiteBusinessIntroductionData.GetList(websiteBusinessCategoryId).Result.Any())
            {
                throw new ArgumentException("此业务分类已被使用，请先删除对应的业务介绍!");
            }
            var result = this.websiteBusinessCategoryData.Delete(websiteBusinessCategoryId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteBusinessCategoryId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取官网业务分类
        public WebsiteBusinessCategoryDto Get(int websiteBusinessCategoryId)
        {
            string cacheKey = this.redisService.GetKey(key, websiteBusinessCategoryId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteBusinessCategoryData.GetEntity(websiteBusinessCategoryId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<WebsiteBusinessCategoryDto>();
        }
        #endregion

        #region 获取官网业务分类列表
        public IEnumerable<WebsiteBusinessCategoryDto> GetList(string tagcode)
        {
            string cacheKey = this.redisService.GetKey(key, tagcode);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.websiteBusinessCategoryData.GetList(tagcode).GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<WebsiteBusinessCategoryDto>>();
        }
        #endregion

        #region 获取官网业务分类分页
        public PageableList<WebsiteBusinessCategoryDto> Query(string tagcode, string title, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key,tagcode,title, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteBusinessCategoryData.Query(tagcode,title,start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                var list = data.Item1.As<IEnumerable<WebsiteBusinessCategoryDto>>();
                                return new PageableList<WebsiteBusinessCategoryDto>
                {
                    Count = data.Item2,
                    Items = list,
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion

        #region 启用官网业务分类
        public bool Enabled(int websiteBusinessCategoryId)
        {
            if (!this.Exists(websiteBusinessCategoryId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteBusinessCategoryData.Enabled(websiteBusinessCategoryId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteBusinessCategoryId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 禁用官网业务分类
        public bool Disabled(int websiteBusinessCategoryId)
        {
            if (!this.Exists(websiteBusinessCategoryId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteBusinessCategoryData.Disabled(websiteBusinessCategoryId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteBusinessCategoryId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion
    }
}